
module ClientManager (insertClient, deleteClient, updateClient, searchClient) where

import Client
import qualified DBS
import qualified StringHelper
import qualified IdObject
import qualified Loan

-- umieszcza klienta w bd-
insertClient :: ClientInfo -> DBS.DBS -> DBS.DBS
insertClient objectInfo db = 
        DBS.setLastClientId newId modifiedDb where
        modifiedDb       = DBS.setClients modifiedObjects db
        modifiedObjects  = objects ++ [newObject]
        objects          = DBS.getClients db
        newObject        = Client newId objectInfo
        newId            = 1 + DBS.getLastClientId db

-- usuwa klienta z danym ID z db -
deleteClient :: DBS.ID -> DBS.DBS -> Either DBS.DBS String
deleteClient i db = 
        if (DBS.selectClientBy ((== i).(IdObject.getId)) db) == [] 
        then Right "nie znaleziono klienta o podanym id"
        else if (DBS.selectLoanBy ((== i).(Loan.getClientId)) db /= []) 
             then Right "znaleziono wypozyczenie odwolujace sie do tego klienta"
             else Left (DBS.DBS (DBS.getLastFilmId db) (DBS.getFilms db) 
                       (DBS.getLastClientId db) (IdObject.remove i (DBS.getClients db)) 
                       (DBS.getLastLoanId db) (DBS.getLoans db) )


{- aktualizuje:
        - atrybut Klienta o danym ID
        - w bazie danych DBS
        - atrybut o nr ID
        - ustawia wartosc val
-}
updateClient :: DBS.ID -> DBS.DBS -> Int -> String -> Either DBS.DBS String
updateClient idClient db idParam newVal = 
        if [foundCilent] == [] then Right "nie znaleziono klienta o podanym id"
        else if (idParam < 1 || idParam > 4) then Right "podano bledny numer atrybutu"
        else Left (DBS.setClients allClients db) where
        otherClients = DBS.selectClientBy ((/= idClient).(IdObject.getId)) db
        [foundCilent] = DBS.selectClientBy ((== idClient).(IdObject.getId)) db
        allClients = otherClients ++ [modifiedClient] where
                modifiedClient    | idParam == 1  = (setName foundCilent newVal)
                                  | idParam == 2  = (setSurname foundCilent newVal)
                                  | idParam == 3  = (setBirthDate foundCilent newVal)
                                  | idParam == 4  = (setAddress foundCilent newVal)
                                  | otherwise     = foundCilent


checkEachClient :: String -> [Client] -> [Client]
checkEachClient s [] = []
checkEachClient s (c:cs) = 
        if StringHelper.isPartOf s (getName c) ||
                StringHelper.isPartOf s (getSurname c) ||
                StringHelper.isPartOf s (getBirthDate c) ||
                StringHelper.isPartOf s (getAddress c) 
        then 
                c : checkEachClient s cs
        else
                checkEachClient s cs

-- szuka w imieniu, nazwisku, dacie urodzenia i adresie
searchClient :: String -> DBS.DBS -> [Client]
searchClient [] db = DBS.getClients db
searchClient s db = checkEachClient s (DBS.getClients db)


